package cn.signalpha.modules.shop.wrapper;

import cn.signalpha.common.constant.MsgConstant;
import cn.signalpha.core.mp.support.BaseEntityWrapper;
import cn.signalpha.core.tool.jackson.JsonUtil;
import cn.signalpha.core.tool.utils.BeanUtil;
import cn.signalpha.core.tool.utils.ObjectUtil;
import cn.signalpha.core.tool.utils.SpringUtil;
import cn.signalpha.core.tool.utils.StringUtil;
import cn.signalpha.modules.com.entity.User;
import cn.signalpha.modules.com.service.UserService;
import cn.signalpha.modules.shop.entity.OrderRefund;
import cn.signalpha.modules.shop.entity.Seller;
import cn.signalpha.modules.shop.entity.SellerAddress;
import cn.signalpha.modules.shop.service.OrderDetailService;
import cn.signalpha.modules.shop.service.OrderRefundLogService;
import cn.signalpha.modules.shop.service.OrderService;
import cn.signalpha.modules.shop.service.SellerService;
import cn.signalpha.modules.shop.vo.OrderGoodsVO;
import cn.signalpha.modules.shop.vo.OrderRefundVO;

import java.util.Objects;

/**
 * 包装类,返回视图层所需的字段
 */
public class OrderRefundWrapper extends BaseEntityWrapper<OrderRefund, OrderRefundVO> {

	private static final UserService userService;
	private static final OrderService orderService;
    private static final SellerService sellerService;
    private static final OrderDetailService orderDetailService;
	private static final OrderRefundLogService orderRefundLogService;


	static {
		userService = SpringUtil.getBean(UserService.class);
		orderService = SpringUtil.getBean(OrderService.class);
        sellerService = SpringUtil.getBean(SellerService.class);
        orderDetailService = SpringUtil.getBean(OrderDetailService.class);
		orderRefundLogService = SpringUtil.getBean(OrderRefundLogService.class);
	}

	public static OrderRefundWrapper build() {
		return new OrderRefundWrapper();
	}

	@Override
	public OrderRefundVO entityVO(OrderRefund orderRefund) {
		OrderRefundVO orderRefundVO = Objects.requireNonNull(BeanUtil.copy(orderRefund, OrderRefundVO.class), MsgConstant.OBJECT_NULL);
		orderRefundVO.setGoods(JsonUtil.parse(orderRefundVO.getGoodsDetail(), OrderGoodsVO.class));
		orderRefundVO.setRefundLogList(orderRefundLogService.listByRefundId(orderRefundVO.getId()));

		User user = userService.getBaseById(orderRefundVO.getUserId());
		if(ObjectUtil.isNotEmpty(user)){
			orderRefundVO.setUserAvatar(user.getAvatar());
			orderRefundVO.setUserNickName(user.getNickName());
			orderRefundVO.setUserMobile(user.getMobile());
		}

		Seller seller = sellerService.getBaseById(orderRefund.getSellerId());
		if(ObjectUtil.isNotEmpty(seller)){
			orderRefundVO.setSellerName(seller.getName());
			orderRefundVO.setSellerLogo(seller.getLogo());
		}

		if(StringUtil.isNotBlank(orderRefundVO.getRefundAddress())){
			orderRefundVO.setRefundAddr(JsonUtil.parse(orderRefundVO.getRefundAddress(), SellerAddress.class));
		}
		return orderRefundVO;
	}

}
